package cn.kgc.novel.controller;

import cn.kgc.novel.common.ErrorCodeEnum;
import cn.kgc.novel.common.Result;
import cn.kgc.novel.pojo.Bookcontext;
import cn.kgc.novel.pojo.Books;
import cn.kgc.novel.pojo.Storage;
import cn.kgc.novel.pojo.Users;
import cn.kgc.novel.service.BookcontextService;
import cn.kgc.novel.service.BooksService;
import cn.kgc.novel.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//杨将
@RestController(value = "bookcontextController")
@CrossOrigin
public class BookcontextController {
    @Resource
    private UsersService usersService;
    @Resource
    private BookcontextService bookcontextService;
    @Resource
    private BooksService booksService;
    //跟据小说名和作者id查数据
    @GetMapping("/selectshow")
    public Result selectshow(String bookname, String name){
        System.out.println("书名："+bookname+"作者:"+name);
        Integer uid = bookcontextService.naid(name);
        List<Bookcontext> selectshow = bookcontextService.selectshow(bookname, uid);
        List<Users> checktheauthor = usersService.checktheauthor(bookname);
        Map<String,Object> maplist = new HashMap<>();
        maplist.put("selectshow",selectshow);
        maplist.put("checktheauthor",checktheauthor);
        return Result.ok(maplist);
    }
    //跟据章节和小说名称那内容
    @GetMapping("/flieshow")
    public Result flieshow(int titleid,String bookname){
        System.out.println(titleid+bookname);
        Bookcontext flieshow = bookcontextService.flieshow(titleid, bookname);
        return Result.ok(flieshow);
    }

    //拿最新更新的章节和时间
    @GetMapping("/getshou")
    public  Result getshow(String category,@RequestParam(defaultValue = "男频") String sex){
        Map<String,Object> map = new HashMap<>();
        List<Storage> boks=new ArrayList<Storage>();
        List<Books> nashijian = booksService.nashijian(category,sex);

        Storage storage = new Storage();
        List<Object> renewaltime = new ArrayList<>();
        for (Books books:nashijian) {
            storage.setRenewaltime(books.getRenewaltime());
            renewaltime.add(books.getRenewaltime());
        }
        List<Bookcontext> nazhanges = bookcontextService.nazhanges(category,sex);
        for (Bookcontext bokk:nazhanges) {
            storage.setTitle(bokk.getTitle());
            storage.setTitleid(bokk.getTitleid());
        }

        map.put("renewaltime",renewaltime);
        map.put("nazhanges",nazhanges);

        return Result.ok(map);
    }


    //增加最新章节
    @PostMapping("/addBookcontext")
    public Result addBookcontext(@RequestBody Bookcontext bookcontext){
        System.out.println("bookcontext:"+bookcontext);
        Result caogaoresult = this.draftchapter(bookcontext.getBid(), bookcontext.getUid(), bookcontext.getTitleid());
        if(caogaoresult==null){
            try {
                bookcontextService.addBookcontext(bookcontext);
                return Result.ok("成功");
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error(ErrorCodeEnum.OPERATION_FAILED);
            }
        }else {
          bookcontextService.updateBookcontext(bookcontext);
            return Result.ok();
        }

    }

    //更据书的id和作者id来查询 这本书的章节
    @GetMapping("/draftchapter")
    public Result draftchapter(Integer bid,Integer uid,Integer titleid){
        List<Bookcontext> draftchapter = bookcontextService.draftchapter(bid, uid,titleid);
        if(draftchapter.size()!=0){
            return Result.ok(draftchapter);
        }else {
            return null;
        }

    }

    //从未发布换成待审核 更书籍id作者id本章序号
    @PostMapping("/updatedraftchapter")
    public Result updatedraftchapter(@RequestBody Bookcontext bookcontext){
        this.addBookcontext(bookcontext);
        try {
            bookcontextService.updatedraftchapter(bookcontext.getBid(),bookcontext.getUid(),bookcontext.getTitleid());

            return Result.ok("成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(ErrorCodeEnum.OPERATION_FAILED);
        }
    }
    //删除草稿
    @RequestMapping("/deletecaogao")
    public Result deletecaogao(Integer uid,Integer bid,Integer titleid){
        int result = bookcontextService.deletecaogao(uid,bid,titleid);
        return Result.ok();
    }

    //查看所有未审核的章节,袁宇灏
    @GetMapping("/findByStatuc")
    public Result findByStatuc(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "5") Integer pageSize)
    {
        PageHelper.startPage(pageNum,pageSize);
        List<Bookcontext> bookcontexts=bookcontextService.findByStatuc();
        PageInfo<Bookcontext> pageInfo=new PageInfo<Bookcontext>(bookcontexts);
        return Result.ok(pageInfo);
    }
    //通过cid查找
    @GetMapping("/findByCid")
    public Result findByCid(Integer cid){
        Bookcontext bookcontext=bookcontextService.findByCid(cid).get(0);
        return Result.ok(bookcontext);
    }
    //审核
//2023-11-23 审核
    @PostMapping("/audit")
    public Result audit(boolean flag,Integer cid,Integer bid,String reason){
        String status=null;
        if (flag){
            status="审核通过";
            booksService.setStatus(bid);
        }else {
            status="审核不通过";
        }
        bookcontextService.audit(status,cid,reason);
        bookcontextService.audit(status,cid,reason);
        return Result.ok();
    }

}
